package com.css.dzj.dzjap.modules.lawenforcesuper.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.executorinfo.entity.ApExecutorInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApExtractLawerEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApExtractLawerDeptService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 安评单位抽取执法人员controller
 * @author limanman
 */
@RequestMapping("/apExtractLawerDept")
@RestController
public class ApExtractLawerDeptController {
    @Autowired
    private ApExtractLawerDeptService apExtractLawerDeptService;
    @Autowired
    BaseService baseService;
    /**
     * 添加两张表
     * @param apExtractLawerEntity
     * @return
     */
    @PostMapping("/saveLawerExtractInfo")
    public R saveLawerExtractInfo(@RequestBody ApExtractLawerEntity apExtractLawerEntity, HttpServletRequest request){
        try {
            R r = apExtractLawerDeptService.saveLawerExtractInfo(apExtractLawerEntity,request);
            if (r.getCode()=="500"){
                baseService.logFailed(3,"添加失败","2");
                return R.error(r.getMsg());
            }else {
                baseService.logSuccess(3,"添加成功","2");
                return R.ok();
            }
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"添加失败","2");
            return  R.error(e.getMessage());
        }
    }

    /**
     * 查询抽取的执法人员
     * @param taskId
     * @param recordId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryLawerExtractOnly")
    public R queryLawerExtractOnly(@RequestParam(required = false) String taskId,
                                   @RequestParam(required = false) String recordId,
                                   @RequestParam(required = false) Integer pageNo,
                                   @RequestParam(required = false) Integer pageSize){
        try {
            ApExtractLawerEntity apExtractLawerEntity = apExtractLawerDeptService.queryLawerExtractOnly(taskId,recordId);
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            if (!StringUtils.isEmpty(apExtractLawerEntity)){
                QueryResults<ApExecutorInfoEntity> apExecutorInfoEntities = apExtractLawerDeptService.queryLawerById(pageRequest,apExtractLawerEntity.getId());
                baseService.logSuccess(3,"查询成功","2");
                return R.ok().put("data",apExecutorInfoEntities).put("apExtractLawerEntity",apExtractLawerEntity);
            }
            baseService.logFailed(3,"查询失败","2");
            return R.error();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return R.error();
        }
    }

}
